package model;

import java.sql.*;

//在这里实现用户登录功能  用户注册功能
public class UserDao {

    //1.根据用户名在数据库中查找，如果有则返回这一条数据
    public User selectByUserName(String username){

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;


        try {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //1.与数据库建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("连接成功");
            //2.根据username在数据库中查询数据
            String sql = "select * from user where username = ? ";
            statement = connection.prepareStatement(sql);
            statement.setString(1,username);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                //如果能查到此用户
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                //3.将查询到的数据通过user对象返回
                return user;
            }else{
                //如果查不到此用户
                return null;
            }



        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;

    }


    //2.插入数据，实现注册功能
    public int RegisterUser(String username, String password){
        //连接数据库
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //1.与数据库建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("连接成功");
            //查看是否已经有该用户，如果有返回2
            String sql1 = "select username from user where username = ? ";
            statement = connection.prepareStatement(sql1);
            statement.setString(1,username);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                if(resultSet.getString("username").equals(username)){
                    return 2;
                }

            }

            //2.根据username在往数据库中插入数据
            String sql = "insert into user values (null,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,password);
            statement.executeUpdate();

            System.out.println("插入成功");

            return 1;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return 0;


    }





}
